
import TaskStep from '@/components/taskStep.vue';
// 请求通用方法
import apiMixin from '@/mixin/apiMixin';
// 任务通用方法
import taskMixin from '@/mixin/taskMixin';
import uilts from '@/utils/index';
export default {
    mixins: [apiMixin,taskMixin],
    data() {
        return {
            loading: false,
            approvalShow: false,
            downTime:0,
            tableData: [],
            searchTypeList: [
                { value: "user_id", name: "用户Id" },
                { value: "task_id", name: "任务Id" }
            ],
            searchType: 'user_id',
            searchText: '',
            listNumsTotal: 0,
            curAuditTask: {
                title: '',
                limited_time: '',
                limited_time_approve: '',
                deadline_time: '',
                custom_steps: [],
                limited_os: []
            },
            uploadBids: [],
            auditStatus: -1,
            refusalCause: '',
            curPage: 1,
            lockBtnLoad: false,
            affirmBtnLoad: false,
            dynamicTags: [],
            inputVisible: false,
            inputValue: ''
        };
    },
    mounted() {
        this.init();
    },
    methods: {
        querySearch(queryString, cb) {//弹框里面的常用回复语言
            var restaurants = [
                "警告！！！很遗憾的告诉您，任务未能通过小二审核，该任务属于违规任务，有问题请联系QQ客服！",
                "很遗憾的告诉您，任务未能通过小二审核，任务分类选择错误，请从新选择分类发布，有问题请联系QQ客服哦。",
                "警告！！！很遗憾的告诉您，任务未能通过小二审核，为防止私下此任务类型不适合私加微信哦！",
                "很遗憾的告诉您，任务未能通过小二审核，该任务佣金过低哦，此任务佣金最低（）元，有问题请联系QQ客服哦。",
                "警告！！！很遗憾的告诉您，任务未能通过小二审核，该任务违规涉及赌博内容，有问题请联系QQ客服哦。",
                "很遗憾的告诉您，任务未能通过小二审核，该任务步骤或任务步骤过程不清晰，请从新发布哦，有问题请联系QQ客服哦。",
                "很遗憾的告诉您，您发任务频率过高，任务未能通过小二审核，建议一小时发布任务小于三个！",
            ];
            const createStateFilter = (queryString) => {
                return restaurants => {
                    return (
                        restaurants.toLowerCase().indexOf(queryString.toLowerCase()) > -1
                    )
                }
            };
            var results = queryString ? restaurants.filter(createStateFilter(queryString)) : restaurants;
            cb(results);
        },
        handleSelect(e) {
            this.refusalCause = e;
            console.log(e);
        },
        custDown() {//弹框倒计时
            let _this = this;
            setTimeout(function() {
                if (_this.downTime > 0) {
                _this.custDown();
                _this.downTime--;
                }
            }, 1000);
        },
        handle(index){//点击审批时的逻辑
            let _this = this;
            _this.tableData[index].btnLoad = true;
            setTimeout(function(){
                _this.approvalShow = true;
                _this.downTime = 6;
                _this.custDown();
                _this.tableData[index].btnLoad = false;
            },1500);
        },
        init() {
            this.initAuditTask();
            this.getAuditTaskListApi();
        },
        initAuditTask() {
            this.curAuditTask = {
                id: '',
                title: '',
                limited_time: '',
                limited_time_approve: '',
                deadline_time: '',
                custom_steps: [],
                limited_os: []
            };
            this.auditStatus = -1;
            this.refusalCause = '';
            this.uploadBids = [];
        },
        auditStatusChange(status) {
            if (status == 1 ) {
                this.refusalCause = '恭喜您，任务已审批通过！';
            }else{
                this.refusalCause = '';
            }
        },
        handleApprovalShow() {
            this.approvalShow = !this.approvalShow;
        },
        levelChange(level) {
            this.dynamicTags.shift();
            this.dynamicTags.unshift(this.taskLevelList[level-1].name);
        },
        auditWill(auditIndex) {
            this.curAuditTask = uilts.simpleCopy(this.tableData[auditIndex]);
            this.dynamicTags =  uilts.simpleCopy(this.tableData[auditIndex].tag);
            this.dynamicTags.unshift('初级任务');
            this.taskOperateLockApi('lock');
            // 判断是否加锁
        },
        startSearch() {
            this.getAuditTaskListApi();
        },
        getAuditTaskListApi() {
            let _self = this;
            this.comResquest(this.request.getAuditTaskList,(res)=>{
                console.log(res);
                //_self.curPage = Number(page) || 1;
                let { count,result } = res;
                _self.listNumsTotal = count;
                _self.tableData = result;
            },{
                page: this.curPage,
                type: this.searchType,
                keyword: this.searchText,
                load: true
            });
        },
        postAuditTask() {
            let _self = this;
            return new Promise((reslove,reject)=>{
                this.comResquest(this.request.postAuditTask,(res,message)=>{
                    if (_self.tableData.length < 2) {
                        _self.curPage = _self.curPage - 1 || 1;
                    }
                    _self.init();
                    _self.handleApprovalShow();
                    _self.$message({
                        message: message,
                        type: 'success'
                    });
                    reslove();
                },{
                    task_id: this.curAuditTask.id,
                    status: this.auditStatus,
                    comment: this.refusalCause,
                },(ero)=>{
                    reject(ero);
                });
            })

        },
        updateTaskDetail() {
            let _self = this;
            let custom_steps = JSON.stringify(this.curAuditTask.custom_steps);
            let update_data = uilts.simpleCopy(this.curAuditTask);
            update_data.custom_steps = custom_steps;
            update_data.code = "";
            update_data.bids = this.uploadBids;
            update_data.tag = JSON.stringify(this.dynamicTags);
            const updateList = () => {
                let changeIndex = 0;
                for (let index = 0;  index < _self.tableData;index++) {
                    if (_self.tableData[index].id == _self.curAuditTask.id) {
                        changeIndex = index;
                        break;
                    }
                }
                _self.$set(_self.tableData,changeIndex,_self.curAuditTask);
            };
            return new Promise((reslove,reject)=>{
                _self.comResquest(_self.request.updateTaskDetail,()=>{
                    updateList();
                    reslove();
                },{
                    id: _self.curAuditTask.id,
                    update_data
                },(ero)=>{
                    reject(ero);
                });
            })
        },
        handleClickAudit() {
            if (this.auditStatus == -1) {
                this.$message({
                    message: '需要选择审核状态',
                    type: 'warning'
                });
                return;
            }
            if (!this.refusalCause) {
                this.$message({
                    message: '需要输入原因',
                    type: 'warning'
                });
                return;
            }
            let _self = this;
            this.affirmBtnLoad = true;
            this.updateTaskDetail().then(()=>{
                _self.postAuditTask().then(()=>{
                    _self.affirmBtnLoad  = false;
                }).catch((ero)=>{
                    _self.affirmBtnLoad  = false;
                    _self.$notify.error({title: '错误',message: ero});
                })
            }).catch((ero)=>{
                _self.affirmBtnLoad  = false;
                _self.$notify.error({title: '错误',message: ero});
            })
        },
        taskOperateLockApi(type) {
            let _self = this;
            let action = type;
            this.comResquest(this.request.taskOperateLock,(res)=>{
                _self.handleApprovalShow();
                if (type === 'unlock') {
                    _self.curAuditTask.id = '';
                }
            },{
                id: this.curAuditTask.id,
                action,
                table: "task",
                load: 'lockBtnLoad'
            },()=>{

            });
        },
        changePage(page){
            this.curPage = page || 1;
            this.getAuditTaskListApi();
        },
        handleClose(tag) {
            this.dynamicTags.splice(this.dynamicTags.indexOf(tag), 1);
        },
        showInput() {
            this.inputVisible = true;
            this.$nextTick(_ => {
                this.$refs.saveTagInput.$refs.input.focus();
            });
        },
        handleInputConfirm() {
            let inputValue = this.inputValue;
            if (inputValue) {
                this.dynamicTags.push(inputValue);
            }
            this.inputVisible = false;
            this.inputValue = '';
        }
    },
    components: {
        TaskStep,
    },
};
